<template>
  <div class="dashboard-container">
    <template>
      <div class="hidden-md-and-up">
        <el-alert title="手机端可能操作不便，建议使用电脑操作" type="warning" />
      </div>
    </template>
    <div class="flex">
      <!-- <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>我的信息</span>
          <el-button style="float: right; padding: 3px 0" type="text">MORE</el-button>
        </div>
        <el-descriptions :column="2" border>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-user" />
              用户名
            </template>
            10204027
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-postcard" />
              教工号
            </template>
            10204027
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-s-custom" />
              姓名
            </template>
            刘建军
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-lock" />
              权限
            </template>
            <el-tag>admin</el-tag>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">
              <i class="el-icon-office-building" />
              部门、院、系
            </template>
            科学研究处、科学技术协会
          </el-descriptions-item>
        </el-descriptions>
      </el-card> -->
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>成果统计</span>
          <el-button style="float: right; padding: 3px 0" type="text">MORE</el-button>
        </div>
        <div id="main" class="echarts-box" />
      </el-card>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>项目统计</span>
          <el-button style="float: right; padding: 3px 0" type="text">MORE</el-button>
        </div>
        <div id="main2" class="echarts-box" />
      </el-card>
    </div>
  </div>
</template>

<script>
import { getTotalAchievement, getTotalProject } from '@/api/user'
import { mapGetters } from 'vuex'

export default {
  name: 'Dashboard',
  computed: {
    ...mapGetters([
      'uid',
      'roles',
      'name'
    ])
  },
  created() {
    const urlParams = new URLSearchParams(window.location.search)
    if (urlParams.has('token')) {
      urlParams.delete('token')
      window.location.href = urlParams.toString() ? '?' + urlParams.toString() : window.location.pathname
    }
  },
  mounted() {
    this.fetchResultsData()
    this.fetchProjectData()
  },
  methods: {
    fetchResultsData() {
      getTotalAchievement().then(response => {
        if (response.code === 200) {
          this.initChart(response.data)
        }
      })
    },
    initChart(data) {
      var chartDom = document.getElementById('main')
      var myChart = this.$echarts.init(chartDom)
      var option
      option = {
        grid: {
          top: '80px',
          left: '10px',
          right: '30px',
          bottom: '80px',
          containLabel: true
        },
        legend: {},
        xAxis: {
          type: 'category',
          data: ['论文', '著作', '专利', '获奖', '艺体类', '咨询报告', '科研平台']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '审核通过',
            type: 'bar',
            data: data[0],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          },
          {
            name: '待校审核',
            type: 'bar',
            data: data[1],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          },
          {
            name: '待院审核',
            type: 'bar',
            data: data[2],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          },
          {
            name: '草稿',
            type: 'bar',
            data: data[3],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          }
        ],
        tooltip: {
          trigger: 'item'
        }
      }
      myChart.setOption(option)
    },
    fetchProjectData() {
      getTotalProject().then(response => {
        if (response.code === 200) {
          this.initChart2(response.data)
        }
      })
    },
    initChart2(data) {
      var chartDom = document.getElementById('main2')
      var myChart = this.$echarts.init(chartDom)
      var option
      option = {
        grid: {
          top: '80px',
          left: '10px',
          right: '10px',
          bottom: '80px',
          containLabel: true
        },
        legend: {},
        xAxis: {
          type: 'category',
          data: ['纵向项目', '横向项目']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '在研待院审核',
            type: 'bar',
            data: data[0],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          },
          {
            name: '在研待校审核',
            type: 'bar',
            data: data[0],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          },
          {
            name: '在研',
            type: 'bar',
            data: data[1],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          },
          {
            name: '结题待院审核',
            type: 'bar',
            data: data[2],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          },
          {
            name: '结题待校审核',
            type: 'bar',
            data: data[2],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          },
          {
            name: '结题',
            type: 'bar',
            data: data[3],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          },
          {
            name: '草稿',
            type: 'bar',
            data: data[4],
            label: {
              show: true,
              position: 'top'
            },
            emphasis: {
              focus: 'item'
            }
          }
        ],
        tooltip: {
          trigger: 'item'
        }
      }
      myChart.setOption(option)
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 20px;
    overflow-x: auto;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}

.echarts-box {
  width: 100%;
  min-width: 650px;
  height: 400px;
}
.flex {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}
.box-card {
  flex: 1;
  min-width: 650px;
}
</style>
